import 'package:distribution_mall_flutter/constant/status_constant.dart';
import 'package:distribution_mall_flutter/entity/order/detail/order_detail_entity.dart';
import 'package:distribution_mall_flutter/entity/order/list/order_list_entity.dart'
    as OrderListEntity;
import 'package:distribution_mall_flutter/http/modules/order.dart';
import 'package:distribution_mall_flutter/utils/debug.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'order_detail.g.dart';

class OrderDetailState {
  OrderDetailEntity? detal;
  bool loading;

  OrderDetailState({this.detal, this.loading = true});

  OrderDetailState.initial() : loading = true;

  OrderDetailState copyWith({OrderDetailEntity? detal, bool? loading}) =>
      OrderDetailState(
        detal: detal ?? this.detal,
        loading: loading ?? this.loading,
      );

  int get getTimeDiffernce {
    if (detal == null) return 0;
    Duration timeDifference = DateTime.fromMillisecondsSinceEpoch(
            detal?.paymentDeadline ?? DateTime.now().millisecondsSinceEpoch)
        .difference(DateTime.now());
    return timeDifference.inSeconds;
  }

  String get paymentMethodName {
    if (detal?.payment?.paidList == null && detal?.payment?.unPayList == null) {
      return '-';
    }

    if (detal?.payment?.paidList?.isNotEmpty == true) {
      return detal?.payment?.paidList?[0].paymentMethodName ?? '-';
    }

    if (detal?.payment?.unPayList?.isNotEmpty == true) {
      return detal?.payment?.unPayList?[0].paymentMethodName ?? '-';
    }

    return '-';
  }

  String get paymentId {
    if (detal?.payment?.paidList == null && detal?.payment?.unPayList == null) {
      return '-';
    }

    if (detal?.payment?.paidList?.isNotEmpty == true) {
      return detal?.payment?.paidList?[0].receiptPaymentId ?? '-';
    }

    if (detal?.payment?.unPayList?.isNotEmpty == true) {
      return detal?.payment?.unPayList?[0].receiptPaymentId ?? '-';
    }

    return '-';
  }

  // 显示支付按钮
  bool get showPay {
    if (detal == null) return false;

    if (detal?.cancelState != OrderStatusEnum.notCancel.status) {
      return false;
    }
    List<PaidListElement>? unPayList = detal?.payment?.unPayList;
    if (unPayList?.isNotEmpty == true &&
        unPayList?[0].paymentType == OrderStatusEnum.offlineSelf.status) {
      if (detal?.state == OrderStatusEnum.toPay.status) return true;
      return false;
    }
    return detal?.state == OrderStatusEnum.toPay.status;
  }

  // 显示取消
  bool get showCancel {
    if (detal == null) return false;

    if (detal?.state == OrderStatusEnum.toPay.status &&
        detal?.cancelState == OrderStatusEnum.notCancel.status) {
      return true;
    }
    return false;
  }

  // 显示物流
  bool get showTrack {
    if (detal == null) return false;

    if (detal?.state != OrderStatusEnum.toPay.status &&
        detal?.state != OrderStatusEnum.closed.status &&
        detal?.state != OrderStatusEnum.waitConfirm.status && 
        detal?.orderType != OrderTypeConstant.sample) {
      return true;
    }
    return false;
  }

  // 显示再次购买
  bool get showBuyAgain {
    if (detal == null) return false;

    if (OrderStatusEnum.getStatusIndex(detal?.state) >=
        OrderStatusEnum.awaitReceive.index) {
      return true;
    }

    return false;
  }
}

final orderDetailProvider =
    StateNotifierProvider.autoDispose<OrderDetailViewModel, OrderDetailState>(
        (ref) {
  return OrderDetailViewModel();
});

class OrderDetailViewModel extends StateNotifier<OrderDetailState> {
  OrderDetailViewModel() : super(OrderDetailState.initial());

  Future<bool> getOrderDetail(String id) async {
    state = state.copyWith(loading: true);
    try {
      OrderDetailEntity? data =
          await OrderApi.orderLoad({'distributionOrderId': id});
      if (data != null) {
        state = state.copyWith(detal: data);
      }
      return true;
    } catch (e) {
      return false;
    } finally {
      debugPrint('+++++++++++++++++++');
      state = state.copyWith(loading: false);
    }
  }

  bool showPayMethod(OrderListEntity.Item data) {
    if (data.cancelState != OrderStatusEnum.notCancel.status) {
      return false;
    }
    List<OrderListEntity.PaidListElement>? unPayList = data.unPayList;
    if (unPayList?.isNotEmpty == true &&
        unPayList?[0].paymentType == OrderStatusEnum.offlineSelf.status) {
      if (data.state == OrderStatusEnum.toPay.status) return true;
      return false;
    }
    return data.state == OrderStatusEnum.toPay.status;
  }

  // 显示取消
  bool showCancelMethod(OrderListEntity.Item data) {
    if (data.state == OrderStatusEnum.toPay.status &&
        data.cancelState == OrderStatusEnum.notCancel.status) {
      return true;
    }
    return false;
  }

  // 显示物流
  bool showTrackMethod(OrderListEntity.Item data) {
    if (data.state != OrderStatusEnum.toPay.status &&
        data.state != OrderStatusEnum.closed.status &&
        data.state != OrderStatusEnum.waitConfirm.status) {
      return true;
    }
    return false;
  }

  // 显示
  bool showMore(OrderListEntity.Item data) {
    if (OrderStatusEnum.getStatusIndex(data.state) >=
            OrderStatusEnum.completed.index &&
        data.orderType != OrderTypeConstant.sample) {
      return true;
    }
    return false;
  }
}

@riverpod
class OrderDetailMerchantAuth extends _$OrderDetailMerchantAuth {
  ///认证状态,默认空字符串是没请求成功.null是未认证
  @override
  String? build() {
    return '';
  }

  void setNewData(String? data) {
    state = data;
  }
}
